package com.shuqi.common;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.os.Environment;

public class FileLog
{
    private static volatile FileLog mLogInstance;
    /**日志保存路径*/
    private static final String LOG_SAVE_PATH = "/shuqi/FileLog/";
    /**日志开关*/
    private static final boolean LOG_SWITCH = true;   //取消LOG

    public static FileLog getInstance(){
        if(mLogInstance == null){
            mLogInstance = new FileLog();
        }
        return mLogInstance;
    }

    /**插入日志*/
    public synchronized void addLog(String logStr){
        if(LOG_SWITCH){
            File file = checkLogFileIsExist();
            if(file == null)
                return;
            FileOutputStream fos = null;
            try
            {
                fos = new FileOutputStream(file, true);
                fos.write((new Date().toLocaleString() + "  " + logStr).getBytes("UTF-8"));
                fos.write("\r\n".getBytes("UTF-8"));
            }
            catch (FileNotFoundException e)
            {
                e.printStackTrace();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            finally{
                try {
                    if(fos != null){
                        fos.close();
                        fos = null;
                    }
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
                fos = null;
                file = null;
            }
        }
    }
    
    /**
     * 插入异常日志
     * @param logStr
     * @param e
     */
    public void addErrorLog(String logStr,Exception e){
    	if(LOG_SWITCH){
	    	String log = logStr + FileLog.getExceptionStackTrace(e);
	    	addLog(log);
    	}
    }

    /**检查日志文件是否存在*/
    private File checkLogFileIsExist(){
        if(!checkSDCard()){
            return null;
        }
        File sdcardDir = Environment.getExternalStorageDirectory();
        File logDir = new File(sdcardDir.getPath() + LOG_SAVE_PATH);
        if(!logDir.exists()){
            logDir.mkdirs();
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = sdf.format(new Date());
        File file = new File(logDir.getPath(),  dateStr + ".txt");
        if(!isLogExist(logDir.getPath(), file)){
            try
            {
                logDir.createNewFile();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        sdf = null;
        return file;
    }

    /**
     * 检查当天日志文件是否存在
     * @param file
     * @return
     */
    private boolean isLogExist(String dir, File file){
        File tempFile = new File(dir);


        File[] files = tempFile.listFiles();
        for(int i = 0; i < files.length; i++){
            if(files[0].getName().trim().equalsIgnoreCase(file.getName())){
                return true;
            }
        }
        return false;
    }

    /**
     * 打印异常堆栈信息
     * @param e
     * @return
     */
    public static String getExceptionStackTrace(Throwable e){
        if(e != null){
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            return sw.toString();
        }
        return "";
    }

    /**
     * 检查SDcard是否存在
     * @return
     */
	private static boolean checkSDCard()
	{
	    if(android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
	       return true;
	    else
	       return false;
	}
}